<html>
<head><meta charset="utf-8"><title>Tuple Limitations · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Tuple.20Limitations.html">Tuple Limitations</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="202804858"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Tuple%20Limitations/near/202804858" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edd Barrett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Tuple.20Limitations.html#202804858">(Jul 03 2020 at 13:42)</a>:</h4>
<p>Hi everyone,</p>
<p>A colleague and I were talking today about the tuple limitations currently in Rust, that you may only have 12 elements max, and that tuples of different lengths are effectively distinct types.</p>
<p>I was wondering: is it therefore impossible to have a function/struct/... parametrised by <code>T</code> where <code>T</code> is a tuple of unspecified structure?</p>
<p>Is this something you'd want to allow in the future? And if so, how would it be implemented?</p>
<p>Thanks</p>



<a name="202804950"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Tuple%20Limitations/near/202804950" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Tuple.20Limitations.html#202804950">(Jul 03 2020 at 13:43)</a>:</h4>
<p>You can have an arbitrary number of elements, the trait impls in libcore just stop at 12. To extend this to arbitrary tuples would require some form of variadic generics.</p>



<a name="202805068"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Tuple%20Limitations/near/202805068" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edd Barrett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Tuple.20Limitations.html#202805068">(Jul 03 2020 at 13:45)</a>:</h4>
<p>OK, I didn't know that! I guess it has to do with the lack of allocation features in <code>core</code>.</p>
<p>What about this part:</p>
<blockquote>
<p>is it therefore impossible to have a function/struct/... parametrised by T where T is a tuple of unspecified structure?</p>
</blockquote>



<a name="202805261"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Tuple%20Limitations/near/202805261" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Tuple.20Limitations.html#202805261">(Jul 03 2020 at 13:47)</a>:</h4>
<p>Tuples aren't heap-allocated, this is just a limitation of the trait system</p>



<a name="202805306"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Tuple%20Limitations/near/202805306" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Tuple.20Limitations.html#202805306">(Jul 03 2020 at 13:47)</a>:</h4>
<blockquote>
<p>I guess it has to do with the lack of allocation features in core.</p>
</blockquote>
<p>It's not about allocation. Tuples generally inherit trait implementations from their members (e.g. <code>(T1, T2, T3)</code> is <code>Debug</code> if <code>T1</code> is <code>Debug</code>, <code>T2</code> is <code>Debug</code> and <code>T3</code> is <code>Debug</code>) and without variadic generics there's no way to provide those implementations for tuples of every size. The implementations in <code>core</code> stop at 12 as an arbitrary choice.</p>



<a name="202805383"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Tuple%20Limitations/near/202805383" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Tuple.20Limitations.html#202805383">(Jul 03 2020 at 13:48)</a>:</h4>
<blockquote>
<p>is it therefore impossible to have a function/struct/... parametrised by T where T is a tuple of unspecified structure?</p>
</blockquote>
<p>Of course, but only up to an arbitrary limit. You can define a trait and implement it for tuples of every size you want or need.</p>



<a name="202806154"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Tuple%20Limitations/near/202806154" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edd Barrett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Tuple.20Limitations.html#202806154">(Jul 03 2020 at 13:57)</a>:</h4>
<p>So I think you are saying it <em>is</em> possible? Can you show me?</p>
<p>Suppose we have a function:</p>
<div class="codehilite"><pre><span></span><code>fn f&lt;T&gt;(x: T) { ... }
</code></pre></div>


<p>And we want <code>f</code> to accept any tuple (and of any length), but reject anything not a tuple?</p>
<p>I didn't think this was possible. How would you access the elements in a type-safe manner?</p>



<a name="202806556"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Tuple%20Limitations/near/202806556" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Tuple.20Limitations.html#202806556">(Jul 03 2020 at 14:01)</a>:</h4>
<p>The problem is the "of any length". That is not currently possible, for the same reason that the impls stop at tuples of size 12.</p>



<a name="202806798"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Tuple%20Limitations/near/202806798" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edd Barrett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Tuple.20Limitations.html#202806798">(Jul 03 2020 at 14:03)</a>:</h4>
<p>I see. Thank you!</p>



<a name="202806812"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Tuple%20Limitations/near/202806812" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Tuple.20Limitations.html#202806812">(Jul 03 2020 at 14:03)</a>:</h4>
<p>IIUC that would require const generics? Is that a use case of const generics?</p>



<a name="202806919"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Tuple%20Limitations/near/202806919" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Tuple.20Limitations.html#202806919">(Jul 03 2020 at 14:04)</a>:</h4>
<p>Ah nevermind, that's tuples</p>



<a name="202806976"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Tuple%20Limitations/near/202806976" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Tuple.20Limitations.html#202806976">(Jul 03 2020 at 14:05)</a>:</h4>
<p>If you want long tuples, you might be interested in <a href="https://docs.rs/frunk/0.3.1/frunk/hlist/index.html">https://docs.rs/frunk/0.3.1/frunk/hlist/index.html</a>, a kind of type-level linked list</p>



<a name="202903325"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Tuple%20Limitations/near/202903325" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Tuple.20Limitations.html#202903325">(Jul 05 2020 at 03:42)</a>:</h4>
<p>being generic over tuples would require some form of variadic generics</p>



<a name="202912042"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Tuple%20Limitations/near/202912042" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Tuple.20Limitations.html#202912042">(Jul 05 2020 at 08:43)</a>:</h4>
<p>I remember an RFC for variadic tuples that never made it to completion</p>



<a name="202912046"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Tuple%20Limitations/near/202912046" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Tuple.20Limitations.html#202912046">(Jul 05 2020 at 08:43)</a>:</h4>
<p>Maybe it’s worth someone picking it up again</p>



<a name="202925216"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Tuple%20Limitations/near/202925216" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Tuple.20Limitations.html#202925216">(Jul 05 2020 at 15:07)</a>:</h4>
<p>every once in a while someone tries again</p>



<a name="202925258"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Tuple%20Limitations/near/202925258" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Tuple.20Limitations.html#202925258">(Jul 05 2020 at 15:08)</a>:</h4>
<p>a VG RFC was <em>the very first issue</em> I made on the <code>rust-lang/rust</code> repo</p>



<a name="202925272"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Tuple%20Limitations/near/202925272" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Tuple.20Limitations.html#202925272">(Jul 05 2020 at 15:08)</a>:</h4>
<p>so that must've been somewhere in late 2013 / early 2014</p>



<a name="202925290"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Tuple%20Limitations/near/202925290" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Tuple.20Limitations.html#202925290">(Jul 05 2020 at 15:09)</a>:</h4>
<p>I even kind of tried to implement it once, before I knew enough about compiler internals :P</p>



<a name="202925310"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Tuple%20Limitations/near/202925310" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Tuple.20Limitations.html#202925310">(Jul 05 2020 at 15:09)</a>:</h4>
<p>I kind of don't see a path for a VG design to be <em>accepted</em></p>



<a name="202925357"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Tuple%20Limitations/near/202925357" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Tuple.20Limitations.html#202925357">(Jul 05 2020 at 15:10)</a>:</h4>
<p>any time soon</p>



<a name="202925388"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Tuple%20Limitations/near/202925388" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Tuple.20Limitations.html#202925388">(Jul 05 2020 at 15:11)</a>:</h4>
<p>maybe after type HRTB (<code>T: for&lt;X: Clone&gt; Trait&lt;X&gt;</code>) and generic closures? since those would allow a certain "functional interface" over tuples and make the nitty gritty details of how it's actually implemented, less important</p>



<a name="202925459"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Tuple%20Limitations/near/202925459" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Tuple.20Limitations.html#202925459">(Jul 05 2020 at 15:13)</a>:</h4>
<p>GATs would also help with certain things, like turning a type like <code>(X, Y, Z)</code> into <code>(&amp;'a X, &amp;'a Y, &amp;'a Z)</code> (by having <code>type Foo&lt;T&gt; = &amp;'a T;</code> in an <code>impl</code>)</p>



<a name="202925506"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Tuple%20Limitations/near/202925506" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Tuple.20Limitations.html#202925506">(Jul 05 2020 at 15:14)</a>:</h4>
<p>and you need stuff like that to express the type of some primitive operations that are largely unimplementable in user code without some kind of reflection and <code>unsafe</code> shenanigans</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>